<?php
class Member extends Vfriend_Db {	
	var $cache;
	var $clear;
	public function __construct() {
		$this->cache 	= new Vfriend_Cache();
		$this->clear 	= $this->cache->clear;
	}
	/**
	 * Dieu kien search
		+ Cung province
		+ Khac gioi
		+ Do tuoi
			- Nu: tuoi hien tai +5
			- Nam: tuoi hien tai -5 tuoi
	 * Order by
		+ Lastlogin
		+ Co hinh
		+ Co chung it nhat 1 so thich
		+ Co chung it nhat 1 nguoi ban chung
	*/
	
	public function match($intUserId, $intAfrom, $intAto, $intGender, $intProvince, $strPreference, $strFriend, $intOffset, $intLimit, &$intTotal=0) {
		try {
			$strKey 	= '';
			$arrResult	= null;
			if($intOffset == 0) {
				$strKey		= MEMBER_MATCH . $intUserId;
				$arrResult 	= $this->cache->get($strKey);
			}
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call users_match(?,?,?,?,?,?,?,?,?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$this->bindParam(2, $intAfrom, PDO::PARAM_INT, 4);
				$this->bindParam(3, $intAto, PDO::PARAM_INT, 4);
				$this->bindParam(4, $intGender, PDO::PARAM_INT, 4);
				$this->bindParam(5, $intProvince, PDO::PARAM_INT, 11);
				$this->bindParam(6, $arrPreference, PDO::PARAM_STR);
				$this->bindParam(7, $strFriend, PDO::PARAM_STR);
				$this->bindParam(8, $intOffset, PDO::PARAM_INT, 11);
				$this->bindParam(9, $intLimit, PDO::PARAM_INT, 4);
				$arrResult 	= $this->fetchAll();
				if($intOffset == 0) {
					$this->cache->set($strKey, $arrResult, MEMBER_MATCH_EXPIRE);
				}
			}
			$intTotal	= isset($arrResult['0']['foundRows']) ? $arrResult['0']['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Member::match - ' .$e->getMessage());			
			return null;
		}
	}
	public function getList($intOffset, $intLimit,&$intTotal=0) {
		try {
			$strKey 	= '';
			$arrResult	= null;
			if($intOffset == 0) {
				$strKey		= MEMBER_GETLIST;
				$arrResult 	= $this->cache->get($strKey);
			}
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call users_getall(?,?)');
				$this->bindParam(1, $intOffset, PDO::PARAM_INT, 11);
				$this->bindParam(2, $intLimit, PDO::PARAM_INT, 4);
				$arrResult 	= $this->fetchAll();
				if($intOffset == 0) {
					$this->cache->set($strKey, $arrResult, MEMBER_GETLIST_EXPIRE);
				}
			}
			$intTotal	= isset($arrResult['0']['foundRows']) ? $arrResult['0']['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Member::getList - ' .$e->getMessage());			
			return null;
		}
	}
	public function search($intUserId, $strKeyword, $intAfrom, $intAto, $intGender, $intAvatar, $strProvince, $strMarital, $strOccupation, $strReligion, $arrPreference, $intOffset, $intLimit, &$intTotal=0) {
		try {			
			$this->prepare('call users_search(?,?,?,?,?,?,?,?,?,?,?,?,?)');
			$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
			$this->bindParam(2, $strKeyword, PDO::PARAM_STR, 50);
			$this->bindParam(3, $intAfrom, PDO::PARAM_INT, 4);
			$this->bindParam(4, $intAto, PDO::PARAM_INT, 4);
			$this->bindParam(5, $intGender, PDO::PARAM_INT, 4);
			$this->bindParam(6, $intAvatar, PDO::PARAM_INT, 4);
			$this->bindParam(7, $strProvince, PDO::PARAM_STR, 255);
			$this->bindParam(8, $strMarital, PDO::PARAM_STR, 255);
			$this->bindParam(9, $strOccupation, PDO::PARAM_STR, 255);
			$this->bindParam(10, $strReligion, PDO::PARAM_STR, 255);
			$this->bindParam(11, $arrPreference, PDO::PARAM_STR, 255);
			$this->bindParam(12, $intOffset, PDO::PARAM_INT, 11);
			$this->bindParam(13, $intLimit, PDO::PARAM_INT, 4);
			$arrResult 	= $this->fetchAll();
			$intTotal	= isset($arrResult['0']['foundRows']) ? $arrResult['0']['foundRows'] : 0;
			return isset($arrResult) ? $arrResult : null;
		} catch(Exception $e) {
			Vfriend_Function::logDb('Member::search - ' .$e->getMessage());			
			return null;
		}
	}
	public function getNewBie($intUserId, $intLimit=MEMBER_NEWBIE_LIMIT, $intDay=MEMBER_NEWBIE_DAY) {
		try {
			$strKey = MEMBER_GETNEWBIE . $intUserId;
			$arrResult	= $this->cache->get($strKey);
			if(!Vfriend_Function::isCached($arrResult)) {
				$this->prepare('call users_getall_newbie(?,?,?)');
				$this->bindParam(1, $intUserId, PDO::PARAM_INT, 11);
				$this->bindParam(2, $intDay, PDO::PARAM_STR, 50);
				$this->bindParam(3, $intLimit, PDO::PARAM_INT, 4);
				$arrResult	= $this->fetchAll();
				
				$this->cache->set($strKey,$arrResult,MEMBER_GETNEWBIE_EXPIRE);
			}
			sort($arrResult);
			return isset($arrResult) ? $arrResult : null;
		}catch(Exception $e) {
			Vfriend_Function::logDb('Member::search - ' .$e->getMessage());			
			return null;
		}
	}
}
?>